﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace GSManager
{
    public class ThoGiaCong
    {
        private string _MaThoGiaCong;
        private string _TenThoGiaCong;
        private string _DiaChi;
        private string _SoDienThoai;
        public string MaThoGiaCong
        {
            get { return _MaThoGiaCong; }
            set { _MaThoGiaCong = value; }
        }
        public string TenThoGiaCong
        {
            get { return _TenThoGiaCong; }
            set { _TenThoGiaCong = value; }
        }
        public string DiaChi
        {
            get { return _DiaChi; }
            set { _DiaChi = value; }
        }
        public string SoDienThoai
        {
            get { return _SoDienThoai; }
            set { _SoDienThoai = value; }
        }
        public void ThemThoThuCong()
        {
            OleDbConnection connect = KetNoi.KetNoiDuLieu();
            string sql = "insert into THOGIACONG values(?,?,?,?)";
            OleDbCommand cmd = new OleDbCommand(sql, connect);
            cmd.Parameters.Add("@MaThoGiaCong", OleDbType.VarChar);
            cmd.Parameters.Add("@TenThoGiaCong", OleDbType.VarWChar);
            cmd.Parameters.Add("@DiaChi", OleDbType.VarWChar);
            cmd.Parameters.Add("@SoDienThoai", OleDbType.VarChar);
            cmd.Parameters[0].Value = MaThoGiaCong;
            cmd.Parameters[1].Value = TenThoGiaCong;
            cmd.Parameters[2].Value = DiaChi;
            cmd.Parameters[3].Value = SoDienThoai;
            cmd.ExecuteNonQuery();
            connect.Close();
        }
        public void XoaThoGiaCong(string matho)
        {
            OleDbConnection connect = KetNoi.KetNoiDuLieu();
            string sql = "Delete From THOGIACONG where MaThoGiaCong='" + matho + "'";
            OleDbCommand cmd = new OleDbCommand(sql, connect);
            cmd.ExecuteNonQuery();
            connect.Close();
        }
        public void CapNhatThongTin()
        {
            OleDbConnection connect = KetNoi.KetNoiDuLieu();
            string sql = "Update THOGIACONG set DiaChi=?,SoDienThoai=? where MaThoGiaCong=?";
            OleDbCommand cmd = new OleDbCommand(sql, connect);
            cmd.Parameters.Add("@DiaChi", OleDbType.VarWChar);
            cmd.Parameters.Add("@SoDienThoai", OleDbType.VarChar);
            cmd.Parameters.Add("@MaThoGiaCong", OleDbType.VarChar);
            cmd.Parameters[0].Value = DiaChi;
            cmd.Parameters[1].Value = SoDienThoai;
            cmd.Parameters[2].Value = MaThoGiaCong;
            cmd.ExecuteNonQuery();
            connect.Close();
        }
        public ThoGiaCong LayThoGiaCong(string matho)
        {
            OleDbConnection connect = KetNoi.KetNoiDuLieu();
            string sql = "select * from THOGIACONG where MaThoGiaCong='" + matho + "'";
            OleDbCommand cmd = new OleDbCommand(sql, connect);
            OleDbDataReader r = cmd.ExecuteReader();
            ThoGiaCong thogiacong = new ThoGiaCong();
            while (r.Read())
            {
                thogiacong.MaThoGiaCong = (string)r[0];
                thogiacong.TenThoGiaCong = (string)r[1];
                thogiacong.DiaChi = (string)r[2];
                thogiacong.SoDienThoai = (string)r[3];
            }
            r.Close();
            connect.Close();
            return thogiacong;
        }
        public ThoGiaCong LayThoGiaCongTheoTen(string tentho)
        {
            OleDbConnection connect = KetNoi.KetNoiDuLieu();
            string sql = "select * from THOGIACONG where TenThoGiaCong=?";
            OleDbCommand cmd = new OleDbCommand(sql, connect);
            cmd.Parameters.Add("@TenThoGiaCong", OleDbType.VarWChar);
            cmd.Parameters[0].Value = tentho;
            OleDbDataReader r = cmd.ExecuteReader();
            ThoGiaCong thogiacong = new ThoGiaCong();
            while (r.Read())
            {
                thogiacong.MaThoGiaCong = (string)r[0];
                thogiacong.TenThoGiaCong = (string)r[1];
                thogiacong.DiaChi = (string)r[2];
                thogiacong.SoDienThoai = (string)r[3];
            }
            r.Close();
            connect.Close();
            return thogiacong;
        }
        public DataSet LayDanhSach()
        {
            OleDbConnection connect = KetNoi.KetNoiDuLieu();
            string sql = "select * from THOGIACONG";
            OleDbDataAdapter adapter = new OleDbDataAdapter(sql, connect);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "THOGIACONG");
            connect.Close();
            return ds;
        }
        public System.Windows.Forms.AutoCompleteStringCollection LayDanhSachTenTho()
        {
            OleDbConnection connect = KetNoi.KetNoiDuLieu();
            string sql = "select TenThoGiaCong,MaThoGiaCong from THOGIACONG";
            OleDbCommand cmd = new OleDbCommand(sql, connect);
            OleDbDataReader r = cmd.ExecuteReader();
            System.Windows.Forms.AutoCompleteStringCollection list = new System.Windows.Forms.AutoCompleteStringCollection();
            while (r.Read())
            {
                string ten = (string)r[0];
                list.Add(ten);
            }
            r.Close();
            connect.Close();
            return list;
        }
        public int DemSoThoGiaCong()
        {
            OleDbConnection connect = KetNoi.KetNoiDuLieu();
            string sql = "select Count(MaThoGiaCong) from THOGIACONG";
            OleDbCommand cmd = new OleDbCommand(sql, connect);
            OleDbDataReader r = cmd.ExecuteReader();
            int kq = 0;
            while (r.Read())
            {
                try
                {
                    kq = (int)r[0];
                }
                catch (Exception) { }
            }
            r.Close();
            connect.Close();
            return kq;
        }
    }
}
